package com.yx.exception;


import com.yx.data.Result;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: 全局异常处理
 * @Auther: zcz
 * @create 2020/4/16 11:51
 */
@RestControllerAdvice
public class GlobalExceptionHandler  {

    /**
     * 处理特定异常
     */
    @ExceptionHandler(BadSqlGrammarException.class)
    public Result error(BadSqlGrammarException e) {
        return Result.error("错误的SQL语句");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result error(HttpMessageNotReadableException e) {
        return Result.error("Json解析异常");
    }

    @ExceptionHandler({UnauthorizedException.class})
    public Result error(UnauthorizedException e) {
        return Result.error(String.format("权限不足[%s]", e.getMessage()));
    }


    @ExceptionHandler({UnauthenticatedException.class})
    public Result error(UnauthenticatedException e) {
        return Result.error("用户认证失败");
    }

    @ExceptionHandler({AuthenticationException.class})
    public Result error(AuthenticationException e) {
        return Result.error("用户认证失败");
    }

    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public Result error(HttpRequestMethodNotSupportedException e) {
        return Result.error("不支持的" + e.getMethod() + "请求方式");
    }

    @ExceptionHandler(value = DuplicateKeyException.class)
    public Result error() {
        return Result.error("存在重复的信息");
    }

    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        return Result.error("未知异常");
    }

}
